var port = require('../../modules/port.js');
var pageNum = 1;
var loadStatus = true;

Page({
    /**
     * 页面的初始数据
     */
    data: {
        articleId: '',
        aboutStatus: false,
        aboutContent: '',
        isPaging: '',
        retate: false,
        articleData: {},
        loadText: '点击加载更多',
        sort: {
            id: '0',
            name: '正序'
        }
    },

    cutstr: function (str, len) {
        var str_length = 0;
        var str_len = 0;
        var str_cut = new String();
        var str_len = str.length;
        for (var i = 0; i < str_len; i++) {
            var a = str.charAt(i);
            str_length++;
            if (escape(a).length > 4) {
                str_length++;
            }
            str_cut = str_cut.concat(a);
            if (str_length >= len) {
                str_cut = str_cut.concat("...");
                return str_cut;
            }
        }
        if (str_length < len) {
            return str;
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (option) {
        wx.showNavigationBarLoading();
        var optId = option.id;
        var that = this;
        this.setData({
            articleId: 12
        })
        wx.request({
            url: port.dataPort.articleInfo,
            data: { articleId: this.data.articleId, pageNum: pageNum, sort: this.data.sort.id },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
            // header: {}, // 设置请求的 header    
            success: function (data) {
                var idx = '';
                var listData = data.data.data.comicList;
                var commentData = data.data.data.comment;
                var vi,c;
                var description = '';
                for (vi in listData) {
                    listData[vi].like = that.numberSwitch(listData[vi].like);
                }
                for (c in commentData) {
                    commentData[c].like = that.numberSwitch(commentData[c].like);
                }
                if (data.data.data.description.length > 130) {
                    description = that.cutstr(data.data.data.description, 130);
                    that.setData({
                        aboutStatus: true,
                        retate: false
                    })
                } else {
                    description = data.data.data.description;
                    that.setData({
                        aboutStatus: false,
                        retate: true
                    })
                }
                that.setData({
                    articleData: data.data.data,
                    aboutContent: description
                })
                wx.hideNavigationBarLoading();
                loadStatus = true;
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    contentTo: function (e) {
        wx.navigateTo({
            url: '../content/content?id=' + this.data.articleId + '&page=' + e.currentTarget.dataset.id
        })
    },
    articleTo: function (e) {
        wx.redirectTo({
            url: '../article/article?id=' + e.currentTarget.dataset.id
        })
    },
    sortBtn: function () {
        if (this.data.sort.id == 0) {
            this.catalogueRequest(this.data.articleData.id, 1, 1);
            pageNum = 1;
            this.setData({
                sort: {
                    id: 1,
                    name: '倒序'
                },
                loadText: '点击加载更多'
            })
        } else {
            this.catalogueRequest(this.data.articleData.id, 1, 0);
            pageNum = 1;
            this.setData({
                sort: {
                    id: 0,
                    name: '正序'
                },
                loadText: '点击加载更多'
            })
        }
    },
    catalogueLoad: function () {
        if (loadStatus == true) {
            loadStatus = false;
            pageNum++;
            this.catalogueRequest(this.data.articleData.id, pageNum, this.data.sort.id);
        }
    },
    catalogueRequest: function (id, page, sort) {
        var that = this;
        wx.showNavigationBarLoading();
        wx.request({
            url: port.dataPort.catalogueRequest,
            data: { articleId: id, pageNum: page, sort: sort },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
            // header: {}, // 设置请求的 header    
            success: function (data) {
                if (data.data.data == null) {
                    that.setData({
                        loadText: '已没有更多内容'
                    })
                }
                var listData = data.data.data;
                var authority = 'articleData.comicList'
                if (page == 1) {
                    for (var vi in listData) {
                        listData[vi].like = that.numberSwitch(listData[vi].like);
                    }
                    that.setData({
                        [authority]: listData
                    })
                } else {
                    var listData = data.data.data;
                    var authority = 'articleData.comicList'
                    var newData = that.data.articleData.comicList;
                    for (var list in listData) {
                        listData[list].like = that.numberSwitch(listData[list].like);
                        newData.push(listData[list]);
                    }
                    that.setData({
                        [authority]: newData
                    })
                }
                wx.hideNavigationBarLoading();
                loadStatus = true;
            }
        })
    },
    aboutMore: function () {
        var aboutText = this.cutstr(this.data.articleData.description, 130)
        if (!this.data.retate) {
            this.setData({
                aboutContent: this.data.articleData.description,
                retate: true
            })
        } else {
            this.setData({
                aboutContent: aboutText,
                retate: false
            })
        }
    },
    numberSwitch: function (str) {
        if (str >= 10000) {
            str = str / 10000;
            return str.toFixed(1) + '万';
        } else {
            return str;
        }
    },
    visitAuthor: function (e) {
        wx.redirectTo({
            url: '../author/author?id=' + e.currentTarget.dataset.id
        })
    }
})